package com.haiheng.module_main.mode

import androidx.lifecycle.MutableLiveData
import com.haiheng.common.api.API
import com.haiheng.common.constant.AppConstant
import com.haiheng.library.log.LogUtils
import com.haiheng.library.log.LogWriter
import com.haiheng.library_base.db.AlarmedBooksTable
import com.haiheng.library_base.http.Domain
import com.haiheng.library_base.http.HttpRequest
import com.haiheng.library_base.http.api
import com.haiheng.library_base.http.postJson
import com.haiheng.library_base.model.BaseViewModel
import com.haiheng.library_base.db.BookInfoBean
import com.haiheng.library_base.db.BookInfoSub
import com.haiheng.library_base.db.KVDatabase
import com.haiheng.module_main.bean.req.AlarmedBooksPageRes
import com.haiheng.module_main.bean.req.PageQueryReq
import com.haiheng.module_main.bean.req.Wherejson

/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2023/6/26 0026
 * @Version: 1.0
 */
class AlarmedBooksViewModel(private val bookInfoViewModel: BookInfoViewModel) : BaseViewModel() {
    var _alarmedBooksData = MutableLiveData<List<AlarmedBooksTable>>(emptyList())

    /**
     * 更新报警列表数据，构建统一书籍列表数据模型
     *
     */
    fun uopdataAlarmedBooks(){
        var list = mutableListOf<BookInfoBean>()
        _alarmedBooksData.value?.forEach {
            var b = BookInfoBean()
            b.barCode = it.sinfo1
            b.bookTitle = it.sinfo2
            b.isAlarmed = true
            var bookInfoSub = BookInfoSub()
            bookInfoSub.name = it.sinfo3
            b.fixedContainerSub = bookInfoSub
            list.add(b)
        }
       bookInfoViewModel._bookListData.postValue(list)
    }
    fun searchAlarmedBooks(){
        api {
            if (AppConstant.isOpenOfflineMode()){
                var alarmedBooks = KVDatabase.getDb().getAlarmedBooksDao().queryAll()
                _alarmedBooksData.postValue(alarmedBooks)
            }else{
                queryAlarmedBooksByNet()
            }
        }
    }
    /**
     * 获取报警书籍列表并同步到本地
     *
     */
    fun queryAlarmedBooksByNet(){
        api {
            var pageQueryReq = PageQueryReq()
            pageQueryReq.page = 1
            pageQueryReq.length = 100000
            var wherejson = arrayListOf<Wherejson>()
            var orgCodejson = Wherejson()
            orgCodejson.conditionalType = 0 //0精确查询，1，模糊查询
            orgCodejson.fieldName = "OrgCode"
            orgCodejson.fieldValue = AppConstant.getLibraryCode()
            wherejson.add(orgCodejson)
            var taskTypejson = Wherejson()
            taskTypejson.conditionalType = 0
            taskTypejson.fieldValue = "AlarmBooks"
            taskTypejson.fieldName = "infoMainType"
            wherejson.add(taskTypejson)
            pageQueryReq.wherejson= wherejson
            val request = HttpRequest(API.PATH.SEARCH_ALARMEDBOOKS)
                .putHeader(Domain.KEY_NAME, Domain.VALUE_DEFUlT)
                .putHeader(API.PARAM.PARAM_HEADER_TOKEN, AppConstant.getToken())
                .setJsonObjec(pageQueryReq)
            var response  = postJson<AlarmedBooksPageRes>(request)
            if (response.isSuccess == true && response.data != null && response.data.size > 0){
                LogUtils.e("查询报警列表成功：${response.data.size}")
                synDataToLocation(response.data)
                _alarmedBooksData.postValue(response.data)
            }else{
                synDataToLocation(listOf())
                LogWriter.e("获取报警列表失败:${response.toJson()}")
            }
        }
    }

    /**
     * 同步报警清单到本地,同步之前会清除之前的数据
     *
     * @param data
     */
    fun synDataToLocation(data: List<AlarmedBooksTable>) {
        api {
            var alarmedBooksDao = KVDatabase.getDb().getAlarmedBooksDao()
            alarmedBooksDao.deleteAll()
            alarmedBooksDao.insertAll(data)
        }
    }
}